package com.demo2do.fms.admin.dao.supplier;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.Level;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.SupplierInfo;
import com.demo2do.fms.entity.supplier.common.SupplierModifiedVersion;
import com.demo2do.fms.entity.supplier.common.SyncStatus;
import com.demo2do.fms.entity.supplier.common.Type;
import com.demo2do.fms.entity.vehicle.ModifiedStatus;

/**
 * 
 * @author Ysy
 *
 */
@Dao("hibernate")
public interface SupplierDao {
	
    /**
     * @return
     */
    @Query("SELECT COUNT(*) FROM Supplier WHERE disabled = false")
    public int count();
    
    /**
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Supplier WHERE disabled = false AND status IN (:status)")
    public int countSupplierByStatus(@Variable("status") AuditStatus[] status);
    
    /**
     * @param status
     * @param syncStatus
     * @return
     */
    @Query("SELECT COUNT(*) FROM Supplier WHERE disabled = false AND status = :status AND syncStatus = :syncStatus")
    public int countSupplierByStatus(@Variable("status") AuditStatus status, @Variable("syncStatus") SyncStatus syncStatus);
    
	/**
	 * @param auditStatus
	 * @param syncStatus
	 * @return
	 */
    @Query("SELECT COUNT(*) FROM Supplier WHERE disabled = false AND status in (:auditStatus) AND syncStatus = :syncStatus")
	public int countSupplierByStatus(@Variable("auditStatus") AuditStatus[] auditStatus, @Variable("syncStatus") SyncStatus syncStatus);
    
    /**
     * @param type
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Supplier WHERE disabled = false AND type = :type AND status IN (:status)")
    public int countSupplierByStatus(@Variable("type") Type type, @Variable("status") AuditStatus[] status);
    
    /**
     * 
     * @param keyword
     * @param type
     * @return
     */
    @Query("SELECT COUNT(*) FROM Supplier WHERE (code = :keyword OR name = :keyword) AND type = :type AND disabled = false")
    public int count(@Variable("keyword") String keyword, @Variable("type") Type type);

    /**
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false")
    public List<Supplier> list();
    
    /**
     * 
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Supplier s WHERE s.disabled = false AND s.status IN (:status)")
    public List<Supplier> listSupplierByPage(@Variable("status") AuditStatus[] status,@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param status
     * @param syncStatus
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Supplier s WHERE s.disabled = false AND s.status = :status AND syncStatus = :syncStatus")
    public List<Supplier> listSupplierByPage(@Variable("status") AuditStatus status, @Variable("syncStatus") SyncStatus syncStatus, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param auditStatus
     * @param aliasOf
     * @param beginIndex
     * @param everyPage
     * @return
     */
    @Limit
    @Query("FROM Supplier s WHERE s.disabled = false AND s.status in (:auditStatus) AND syncStatus = :syncStatus")
	public List<Supplier> listSupplierByPage(@Variable("auditStatus") AuditStatus[] auditStatus, @Variable("syncStatus") SyncStatus syncStatus, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int everyPage);
    
    /**
     * @param type
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Supplier WHERE disabled = false AND type = :type AND status in (:status)")
    public List<Supplier> listSupplierByPage(@Variable("type") Type type, @Variable("status") AuditStatus[] status,@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param level
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false AND level = :supplierLevel")
    public List<Supplier> list(@Variable("supplierLevel") Level level);
    
    /**
     * @param level
     * @param type
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false AND level = :level AND type = :type")
    public List<Supplier> list(@Variable("level") Level level, @Variable("type") Type type);
    
    /**
     * 
     * @param ids
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false AND id IN(:ids) ORDER BY lastModifyTime DESC")
    public List<Supplier> list(@Variable("ids") List<Long> ids);
    
    /**
     * @param type
     * @param status
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false AND type = :type AND status IN (:status) ORDER BY lastModifyTime DESC")
    public List<Supplier> list(@Variable("type") Type type, @Variable("status") List<AuditStatus> status);
    
    /**
     * @param type
     * @param status
     * @param branch
     * @return
     */
    @Query("FROM Supplier WHERE disabled = false AND type = :type AND status IN (:status) AND branch = :branch ORDER BY lastModifyTime DESC")
    public List<Supplier> list(@Variable("type") Type type, @Variable("status") List<AuditStatus> status, @Variable("branch") Branch branch);
    
    /**
     * @param type
     * @return
     */
    @Query("FROM Supplier WHERE type = :type AND disabled = false ORDER BY lastModifyTime DESC")
    public List<Supplier> list(@Variable("type") Type type);
    
    /**
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Supplier s WHERE s.disabled = false ORDER BY s.lastModifyTime DESC")
    public List<Supplier> list(@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @return
     */
    @Query("SELECT id FROM Supplier WHERE disabled = false ORDER BY lastModifyTime DESC")
    public List<Long> listSupplierIds();

    /**
     * @param supplierId
     * @return
     */
    @Query("FROM Supplier WHERE id = :supplierId AND disabled = false")
    public Supplier loadSupplier(@Variable("supplierId") Long supplierId);
    
    /**
     * @param orgId
     * @param type
     * @return
     */
    @Query("FROM Supplier s WHERE s.disabled = false AND s.orgId = :orgId AND s.type = :type ")
    public Supplier loadSupplier(@Variable("orgId") Long orgId , @Variable("type") Type type);
    
    /**
     * @param supplier
     * @return
     */
    @Query("FROM SupplierInfo WHERE supplier = :supplier AND disabled = false")
    public SupplierInfo loadSupplierInfo(@Variable("supplier") Supplier supplier);
    
    /**
     * 
     * @param supplierId
     * @param status
     * @return
     */
    @Query("FROM SupplierModifiedVersion WHERE supplierId = :supplierId AND status = :status ORDER BY versionSorting DESC")
    public SupplierModifiedVersion loadModifiedVersion(@Variable("supplierId") Long supplierId, @Variable("status") ModifiedStatus status);
    
    /**
     * 
     * @param status
     * @param supplierId
     * @param auditing
     */
    @Execute("UPDATE SupplierModifiedVersion SET status = :status WHERE supplierId = :supplierId AND status = :auditing")
    public void updateModifiedVersion(@Variable("status") ModifiedStatus status, @Variable("supplierId") Long supplierId, @Variable("auditing") ModifiedStatus auditing);


}
